<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-13
 * Time: 下午6:46
 */

namespace backend\controllers;


use common\models\Code;
use common\service\CommonService;
use common\service\ConstantService;
use common\service\SmsService;

class CodeController extends BaseController
{
    /**
     * 发送短信验证码
     */
    public function actionSendCode(){
        if($this->isPost() && $this->isAjax()){
            $phone = $this->post('phone');
            if(!preg_match('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$/',$phone)){
                return $this->renderJson([],'请输入正确的电话号码',ConstantService::$response_fail);
            }
            if(!$phone){
                return $this->renderJson([],'参数错误',ConstantService::$response_fail);
            }
            $data = Code::find()->where(['phone'=>$phone])->orderBy(['id'=>SORT_DESC])->one();
            if((time()-$data['time'])>300){
                $code = SmsService::addSms($phone);
//                $mre = CommonService::sendSms($phone,$code);
                //调用发送短信方法
                if($code ){
                    return $this->renderJson([],'发送成功',ConstantService::$response_success);
                }else{
                    return $this->renderJson([],'系统错误稍后再试',ConstantService::$response_fail);
                }
            }else{
                return $this->renderJson([],'请5分钟后再试一试',ConstantService::$response_fail);
            }
        }
        return $this->renderJson([],'接口错误',ConstantService::$response_fail);
    }

}